/**
 * 
 */
package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.File;
import cn.gov.msa.model.ServiceGuide;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */

public class DeclareServcie {
	
	

	
	public static String list(ApplicationContext ctx, String params) throws Exception {
		String result = "";
		
		ObjectMapper mapper = new ObjectMapper();
		
		cn.gov.msa.service.DeclareItemService declareItemService = (cn.gov.msa.service.DeclareItemService) ctx.getBean("DeclareItemService");  	
		JSONObject jsonParams = JSONObject.fromObject(params);
		int pageNum = 1;
		int pageSize = 10;
		if(jsonParams.has("pageNum"))
		{
			pageNum = jsonParams.getInt("pageNum");
			jsonParams.remove("pageNum");
		}
		if(jsonParams.has("pageSize"))
		{
			pageSize = jsonParams.getInt("pageSize");
			jsonParams.remove("pageSize");
		}
		
		DeclareItem declareItem = mapper.readValue(jsonParams.toString(), DeclareItem.class);
				
		List<DeclareItem> declareItemList = declareItemService.selectData(pageNum, pageSize, declareItem);
	    //获取分页信息
        PageInfo page = new PageInfo(declareItemList);
        

		JSONArray ja = JSONArray.fromObject(declareItemList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	
	public static String getguideinfo(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = "";
		JSONObject json = new JSONObject();
		cn.gov.msa.service.DeclareItemService declareItemService = (cn.gov.msa.service.DeclareItemService) ctx.getBean("DeclareItemService");  	
		
		try{
			ServiceGuide serviceGuide = new ServiceGuide();
			
			serviceGuide = declareItemService.selectByPrimaryKey(params);
			json = JSONObject.fromObject(serviceGuide);
			
			result = serviceGuide.getItemno() != null ? 1 : 0;
			info = serviceGuide.getItemno() != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
		}
		catch(Exception e){
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", json);
				
		return jsonResult.toString();
	}
	
	

	public static String guidetable(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		JSONObject jsonParams = JSONObject.fromObject(params);
		try{
		
			String[] fileids = null;
			if(jsonParams.has("fileid"))
				fileids = jsonParams.getString("fileid").split(",");
	
	
			JSONArray ja = getFile(ctx,fileids);
			
			result = ja.size() > 0 ? 1 : 0;
			info = ja.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			jsonResult.put("rows", ja);
			jsonResult.put("total", fileids.length);
		}
		catch(Exception e){
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		return jsonResult.toString();
		
	}
	
	
	@RequestMapping(value = "/guidestyle")
	public static String guidestyle(ApplicationContext ctx, String params) throws Exception {

		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		JSONObject jsonParams = JSONObject.fromObject(params);
		
		try{
		
			String[] fileids = null;
			if(jsonParams.has("fileid"))
				fileids = jsonParams.getString("fileid").split(",");
	
	
			JSONArray ja = getFile(ctx,fileids);
			
			result = ja.size() > 0 ? 1 : 0;
			info = ja.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			jsonResult.put("rows", ja);
			jsonResult.put("total", fileids.length);
		}
		catch(Exception e){
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		return jsonResult.toString();
	}
	
	private static JSONArray getFile(ApplicationContext ctx,String[] fileids) throws Exception{
		JSONArray ja = new JSONArray();
		
		cn.gov.msa.service.DeclareItemService declareItemService = (cn.gov.msa.service.DeclareItemService) ctx.getBean("DeclareItemService");  	
		for (String fileId : fileids) {
			File file = declareItemService.selectFile(fileId);
			if (file!=null) {
				JSONObject jsonFile = new JSONObject();
				jsonFile.put("caption", file.getFilename() + "." + file.getFilesuffix());
				jsonFile.put("size", file.getFilesize());
				jsonFile.put("width", "120px");
				jsonFile.put("url", file.getFileurl());
				jsonFile.put("key", file.getFileid());	
				ja.add(jsonFile);
			}
		}
		
		return ja;
	}
	/*@RequestMapping(value = "/getInfo")
	public String getInfo(String itemno) throws Exception {
		int result = 0;
		String info = "";
		
		JSONObject joparam = new JSONObject();
		
		try{

			DeclareItem declareItem = declareItemService.getInfoByItemno(itemno);		
			
			result = declareItem != null ? 1 : 0;
			info = declareItem != null ? "查询成功！" : "删除无数据！";
			
			joparam = JSONObject.fromObject(declareItem);
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			//得到明细的model数据
			JSONArray jaDeclareItemMaterial = param.getJSONObject("table").getJSONArray("row");
			List<DeclareItemMaterial> declareItemMaterialList = new ArrayList<DeclareItemMaterial>();
			for (Object declareItemMaterialObj : jaDeclareItemMaterial) {
				DeclareItemMaterial declareItemMaterial = mapper.readValue(declareItemMaterialObj.toString(), DeclareItemMaterial.class);
				
				declareItemMaterialList.add(declareItemMaterial);
			}
			
			//得到类型的model数据
			param.remove("table");			
			DeclareItem declareItem = mapper.readValue(param.toString(), DeclareItem.class);	
		    
		    save = declareItemService.updateData(declareItem, declareItemMaterialList);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = InfoObjUtil.Success_save_OK;
	    }
	    else{
	    	result = 0;
	    	info = InfoObjUtil.Error_save_no;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getMaterial")
	public String getMaterial(String itemno) throws Exception {
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{

			List<DeclareItemMaterial> declareItemMaterialList = declareItemMaterialService.selectDeclareItemMaterial(itemno);
			
			result = declareItemMaterialList != null && declareItemMaterialList.size() > 0 ? 1 : 0;
			info = declareItemMaterialList != null && declareItemMaterialList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			japaram = JSONArray.fromObject(declareItemMaterialList);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}

	@RequestMapping(value = "/save")
	public String save(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			//得到明细的model数据
			JSONArray jaDeclareItemMaterial = param.getJSONObject("table").getJSONArray("row");
			List<DeclareItemMaterial> declareItemMaterialList = new ArrayList<DeclareItemMaterial>();
			for (Object declareItemMaterialObj : jaDeclareItemMaterial) {
				DeclareItemMaterial declareItemMaterial = mapper.readValue(declareItemMaterialObj.toString(), DeclareItemMaterial.class);
				
				declareItemMaterialList.add(declareItemMaterial);
			}
			
			//得到类型的model数据
			param.remove("table");			
			DeclareItem declareItem = mapper.readValue(param.toString(), DeclareItem.class);	
		    
		    save = declareItemService.saveData(declareItem, declareItemMaterialList);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = InfoObjUtil.Success_save_OK;
	    }
	    else{
	    	result = 0;
	    	info = InfoObjUtil.Error_save_no;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/delete")
	public String delete(String itemno) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{
			int delete = declareItemService.delete(itemno);	
			
			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? InfoObjUtil.Success_save_OK : InfoObjUtil.Error_save_no;
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/batchDelete")
	public String batchDelete(String itemnos) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{			
			String itemnoResult = "";
			
			for(int i = 0 ; i < itemnos.split(",").length; i++){	
				String itemno = itemnos.split(",")[i];
				int delete = declareItemService.delete(itemno);
				
				if(delete > 0)
					itemnoResult += itemno + ",";
			}
			
			itemnoResult = itemnoResult.substring(0, itemnoResult.length() - 1);

			JSONObject param = new JSONObject();
			param.put("itemno", itemnoResult);
			japaram.add(param);
			
			result = itemnoResult.split(",").length > 0 ? 1 : 0;
			info = itemnoResult.split(",").length > 0 ? "批量删除成功！" : "批量删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);		
		
		return jsonResult.toString();
	}
	
	
	@RequestMapping(value = "/saveguide")
	public String saveguide(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
		
	    try{
			//得到model数据
			ServiceGuide serviceGuide = mapper.readValue(param.toString(), ServiceGuide.class);	
			BigDecimal b1 = new BigDecimal(Double.toString(1));   
			serviceGuide.setSysorder(b1);
		    
			save = guideService.saveData(serviceGuide);
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    
	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = InfoObjUtil.Success_save_OK;
	    }
	    else{
	    	result = 0;
	    	info = InfoObjUtil.Error_save_no;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@Autowired
	FileMapper fileMapper;

	
	
	private JSONArray getFile(String[] fileids) throws Exception{
		JSONArray ja = new JSONArray();
		
		for (String fileId : fileids) {
			File file = fileMapper.selectFile(fileId);

			JSONObject jsonFile = new JSONObject();
			jsonFile.put("caption", file.getFilename() + "." + file.getFilesuffix());
			jsonFile.put("size", file.getFilesize());
			jsonFile.put("width", "120px");
			jsonFile.put("url", file.getFileurl());
			jsonFile.put("key", file.getFileid());
			
			ja.add(jsonFile);
		}
		
		return ja;
	}

	@RequestMapping(value = "/guidestyle")
	public String guidestyle(@RequestBody JSONObject params) throws Exception {

		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		
		try{
		
			String[] fileids = null;
			if(params.has("fileid"))
				fileids = params.getString("fileid").split(",");
	
	
			JSONArray ja = getFile(fileids);
			
			result = ja.size() > 0 ? 1 : 0;
			info = ja.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			jsonResult.put("datas", ja);
		}
		catch(Exception e){
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		return jsonResult.toString();
	}
	

	@Autowired
	ServiceGuideMapper serviceGuideMapper;

	@RequestMapping(value = "/getguideinfo")
	public String getguideinfo(String sgid) throws Exception {
		int result = 0;
		String info = "";
		JSONObject json = new JSONObject();
		
		try{
			ServiceGuide serviceGuide = new ServiceGuide();
//			serviceGuide.setItemno(itemno);
			
//			List<ServiceGuide> serviceGuideList = guideService.selectData(serviceGuide);
//	
//			if(serviceGuideList.size() > 0)
//				json = JSONObject.fromObject(serviceGuideList.get(0));
			
			serviceGuide = serviceGuideMapper.selectByPrimaryKey(sgid);
			json = JSONObject.fromObject(serviceGuide);
			
			result = serviceGuide.getItemno() != null ? 1 : 0;
			info = serviceGuide.getItemno() != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
		}
		catch(Exception e){
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", json);
		
		
		
		return jsonResult.toString();
	}*/
}
